<?php
final class pfRendererHTML implements pfRendererField, pfRendererForm, pfRendererLabel {

	static private $instance;
	
	static public function getInstance() {
		if (self::$instance == null) {
			self::$instance = new self;
		}
		return self::$instance;
	}

	static public function renderStartTag(pfForm $form) {

	}

	static public function renderEndTag(pfForm $form) {

	}
	
	private function __construct() {
		
	}

	public function renderField(pfField $field, pfFieldValue $value = null) {
		switch($field->getType()) {
			case pfField::TYPE_TEXTAREA:
				$html = '';
				break;
			default:
				$html = '<input type="' . $field->getType() . '" id="' . $field->getId() . '" name="' . $field->getName() . '" />';
				break;
		}		
		return $html;
	}

	public function printField(pfField $field, pfFieldValue $value = null) {
		echo $this->render($field);
	}

	public function renderLabel(pfLabel $label, pfField $field) {
		//quoting
		return '<label for="' . $field->getId() . '">' . $label->getText() . '</label>';
	}

	public function printLabel(pfLabel $label, pfField $field) {
		echo $this->renderLabel($label, $field);
	}

	public function renderForm(pfForm $form, pfFormValue $value = null) {

		$fieldRenderer = pfRendererFieldHTML::getInstance();
		
		$html = array();

		$html['start'] = '<form action="">';

		foreach($form as $field) {
			$html[] = $fieldRenderer->render($field);
		}
		
		$html['end'] = '</form>';

		return $html;
	}

	public function printForm(pfForm $form, pfFormValue $value = null) {
		//hier behavior für drumherum
		$elements = $this->render($form, $value);
		
		$startTag = array_shift($elements);
		$endTag   = array_pop($elements);
		
		$html  = '';
		$html .= $startTag;

		foreach ($elements as $key => $element) {
			$html .= $element;
		}

		$html .= $endTag;

		return $html;
	}

}
